﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;

namespace GFluentCode.Tools
{
    /// <summary>
    /// 图像帮助类
    /// </summary>
    public class ImageHelper
    {
        #region base64互转

        /// <summary>
        /// 图像转base64
        /// </summary>
        /// <param name="imgPath"></param>
        /// <returns></returns>
        public static string ToBase64(string imgPath)
        {
            string strbaser64 = "";

            try
            {
                Bitmap bmp = new Bitmap(imgPath);
                using (MemoryStream ms = new MemoryStream())
                {
                    bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                    byte[] arr = new byte[ms.Length];
                    ms.Position = 0;
                    ms.Read(arr, 0, (int)ms.Length);
                    ms.Close();

                    strbaser64 = Convert.ToBase64String(arr);
                }
            }
            catch (Exception)
            {
                throw new Exception("Something wrong during convert!");
            }

            return strbaser64;
        }
        /// <summary>
        /// base64转图像
        /// </summary>
        /// <param name="base64"></param>
        /// <returns></returns>
        public static Image FromBase64(string base64)
        {
            byte[] b = Convert.FromBase64String(base64);
            MemoryStream ms = new MemoryStream(b);
            Bitmap bitmap = new Bitmap(ms);
            return bitmap;
        }
        /// <summary>
        /// 流转图像
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static Image FromStream(byte[] stream)
        {
            var base64 = Encoding.UTF8.GetString(stream);
            byte[] b = Convert.FromBase64String(base64);
            MemoryStream ms = new MemoryStream(b);
            Bitmap bitmap = new Bitmap(ms);
            return bitmap;
        }
        #endregion

        #region 图像压缩

        /// <summary>
        /// 
        /// </summary>
        /// <param name="orgPath">原始图像地地</param>
        /// <param name="width">新宽度</param>
        /// <param name="height">新高度</param>
        /// <param name="path">新图像地址</param>
        /// <param name="imgFormat">图像类型</param>
        public static void Compress(string orgPath, int width, int height, string path, ImageFormat imgFormat)
        {
            using (var imgPic = new Bitmap(orgPath))
            {
                Compress(imgPic, width, height, path, imgFormat);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="orgPath"></param>
        /// <param name="precent">压缩百分比</param>
        /// <param name="path"></param>
        /// <param name="imgFormat"></param>
        public static void Compress(string orgPath, double precent, string path, ImageFormat imgFormat)
        {
            using (var imgPic = new Bitmap(orgPath))
            {
                Compress(imgPic, (int)(imgPic.Width * precent), (int)(imgPic.Height * precent), path, imgFormat);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="img"></param>
        /// <param name="precent"></param>
        /// <param name="path"></param>
        /// <param name="imgFormat"></param>
        public static void Compress(Image img, double precent, string path, ImageFormat imgFormat)
        {
            Compress(img, (int)(img.Width * precent), (int)(img.Height * precent), path, imgFormat);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="img">原始图像</param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="path"></param>
        /// <param name="imgFormat"></param>
        public static void Compress(Image img, int width, int height, string path, ImageFormat imgFormat)
        {
            using (var bmp = new Bitmap(width, height))
            {
                var g = Graphics.FromImage(bmp);
                g = Graphics.FromImage(bmp);
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                g.DrawImage(img, new Rectangle(0, 0, width, height));
                bmp.Save(path, imgFormat);
                bmp.Dispose();
                g.Dispose();

            }
        }
        #endregion
    }
}
